<?php

class MeetsController extends AppController {

    var $name = 'Meets';
    var $helpers = array('paginator', 'Html', 'Form', 'Ajax', 'Javascript','DatePicker');
    var $components = array('RequestHandler');
    var $paginate = array(
        'limit' => 4,
        'order' => array(
            'Meet.id' => 'asc'
        )
    );

    function index() {
        $this->Meet->recursive = 1;
        $this->set('meets', $this->paginate());
        $current_time = date('Y-m-d H:i:s');
        $toans = $this->Meet->find('all', array(
            'conditions' => array(
                'Meet.time_start >' => date('Y-m-d H:i:s'),
            )
                ));
        $this->set('toans', $toans);
        $this->set('current', $current_time);
    }

    function sample() {
        $this->Meet->recursive = 1;
        $this->set('meets', $this->paginate());
        $current_time = date('Y-m-d H:i:s');
        $toans = $this->Meet->find('all', array(
            'conditions' => array(
                'Meet.time_start >' => date('Y-m-d H:i:s'),
            )
                ));
        $this->set('current', $current_time);
    }

    function home() {
        $this->Meet->recursive = 1;
        $this->set('meets', $this->paginate());
        $current_time = date('Y-m-d H:i:s');
        $toans = array(
            'Meet.time_start >' => date('Y-m-d H:i:s'),
        );
        $this->set('toans', $this->paginate('Meet', $toans));
        $this->set('current', $current_time);
        $chiens = array(
            'Meet.time_start <' => date('Y-m-d H:i:s'),
        );
        $this->set('chiens', $this->paginate('Meet', $chiens));
    }

    function view($id = null) {
        $this->Meet->recursive = 2;
        if (!$id) {
            $this->Session->setFlash(__('Invalid meet', true));
            $this->redirect(array('action' => 'index'));
        }
        $time_start = $this->Meet->find('all', array(
            'fields' => 'time_start',
            'conditions' => array('Meet.id' => $id),
                )
        );
        $this->set('time_start', $time_start);
        $this->set('meet', $this->Meet->read(null, $id));
        $this->set('status', $this->info($id));
        //$this->set('time_now', getdate());
    }

    function add() {
        if (!empty($this->data)) {
            $this->Meet->create();
            if ($this->Meet->save($this->data)) {
                $this->Session->setFlash(__('The meet has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The meet could not be saved. Please, try again.', true));
            }
        }
        $rooms = $this->Meet->Room->find('list', array('fields' => 'Room.room_name'));
        $devices = $this->Meet->Device->find('list', array('fields' => 'Device.device_name'));
        $departments = $this->Meet->Staff->Department->find('list',array('fields'=>'Department.department_name'));
        //$staffs = $this->Meet->Staff->find('list', array('fields' => array('Staff.staff_name')));
        $staffs =  array();
        $tests = array();
        $this->set(compact('rooms', 'devices', 'staffs', 'departments','tests'));
    }

    function getStaffs() {
        $this->set('options', $this->Meet->Staff->find('list', array(
                    'conditions' => array(
                        'Staff.department_id' => $this->data['Meet']['department_id']
                    ),
                    'fields' => array('Staff.staff_name')
                        )
                )
        );
        $this->render('/meets/ajax_dropdown');
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid meet', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Meet->save($this->data)) {
                $this->Session->setFlash(__('The meet has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The meet could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Meet->read(null, $id);
        }
        $rooms = $this->Meet->Room->find('list');
        $devices = $this->Meet->Device->find('list');
        $staffs = $this->Meet->Staff->find('list');
        $this->set(compact('rooms', 'devices', 'staffs'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for meet', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Meet->delete($id)) {
            $this->Session->setFlash(__('Meet deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Meet was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function info($id = null) {
        $status = 'sap dien ra';
        $time_starts = $this->Meet->find('all', array(
            'fields' => 'time_start',
            'conditions' => array('Meet.id' => $id),
                )
        );
        foreach ($time_starts as $time_start) {
            if ($time_start['Meet']['time_start'] < date('Y-m-d H:i:s')) {
                $status = 'da dien ra';
            }
        }

        return $status;
    }

}
